﻿using Ldt.Aop;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using QrCodeService.Bll;
using QrCodeService.Model;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace QrCodeService.Webapi.Controllers
{
    /// <summary>
    /// Class QrCodeMainController.
    /// Implements the <see cref="Microsoft.AspNetCore.Mvc.ControllerBase" /></summary>
    /// <seealso cref="Microsoft.AspNetCore.Mvc.ControllerBase" />
    [StatisticalTimeOnlyMethod(IsLogException = false, LogLevelItem = "Info")]
    [ApiController]
    [Route("api")]
    public class QrCodeMainController : ControllerBase
    {
        private readonly ILogger<QrCodeMainController> logger;
        private readonly QrCodeHelper qrCodeHelper;
        /// <summary>
        /// Initializes a new instance of the <see cref="QrCodeMainController"/> class.
        /// </summary>
        /// <param name="logger">The logger.</param>
        /// <param name="qrCodeHelper"></param>
        public QrCodeMainController(ILogger<QrCodeMainController> logger, QrCodeHelper qrCodeHelper)
        {
            this.logger = logger;
            this.qrCodeHelper = qrCodeHelper;
        }
        /// <summary>
        /// 二维码识别
        /// </summary>
        /// <param name="formCollection"></param>
        /// <returns></returns>
        [HttpPost("QrcodeRecognition")]
        public async Task<ActionResult> QrcodeRecognition([FromForm] IFormCollection formCollection)
        {
            HttpReturnEntity httpReturnEntity = new HttpReturnEntity() { Code = "1" };
            FormFileCollection fileCollection = (FormFileCollection)formCollection.Files;
            if (fileCollection.Count == 0)
            {
                throw new Exception("没有上传图片文件");
            }
            using var file = fileCollection.First().OpenReadStream();
            byte[] buffer = new byte[file.Length];
            file.Read(buffer, 0, buffer.Length);
            httpReturnEntity.Body = qrCodeHelper.Detect1(buffer);
            if (httpReturnEntity.Body == null)
            {
                httpReturnEntity.Body = qrCodeHelper.Detect2(buffer);
            }
            await Task.CompletedTask;
            return Ok(httpReturnEntity);
        }
        /// <summary>
        /// 二维码识别1
        /// </summary>
        /// <param name="formCollection"></param>
        /// <returns></returns>
        [HttpPost("recognition1")]
        public async Task<ActionResult> QrcodeRecognition1([FromForm] IFormCollection formCollection)
        {
            HttpReturnEntity httpReturnEntity = new HttpReturnEntity() { Code = "1" };
            FormFileCollection fileCollection = (FormFileCollection)formCollection.Files;
            if (fileCollection.Count == 0)
            {
                throw new Exception("没有上传图片文件");
            }
            using var file = fileCollection.First().OpenReadStream();
            byte[] buffer = new byte[file.Length];
            file.Read(buffer, 0, buffer.Length);
            httpReturnEntity.Body = qrCodeHelper.Detect1(buffer);
            await Task.CompletedTask;
            return Ok(httpReturnEntity);
        }
        /// <summary>
        /// 二维码识别2
        /// </summary>
        /// <param name="formCollection"></param>
        /// <returns></returns>
        [HttpPost("recognition2")]
        public async Task<ActionResult> QrcodeRecognition2([FromForm] IFormCollection formCollection)
        {
            HttpReturnEntity httpReturnEntity = new HttpReturnEntity() { Code = "1" };
            FormFileCollection fileCollection = (FormFileCollection)formCollection.Files;
            if (fileCollection.Count == 0)
            {
                throw new Exception("没有上传图片文件");
            }
            using var file = fileCollection.First().OpenReadStream();
            byte[] buffer = new byte[file.Length];
            file.Read(buffer, 0, buffer.Length);
            httpReturnEntity.Body = qrCodeHelper.Detect2(buffer);
            await Task.CompletedTask;
            return Ok(httpReturnEntity);
        }
    }
}
